﻿using ConfExpert.Utility;
using DevExpress.Utils;
using DevExpress.XtraBars;
using DevExpress.XtraGrid.Views.Grid;
using DevTeam;
using Extensions;
using System;
using System.Data;
using System.Drawing;

namespace ConfOrder
{
    public partial class ArticleOrders : XForm
    {
        public ArticleOrders()
        {
            InitializeComponent();
        }

        DataTable dtAll = AppSession.DataManager.ExecuteDataTable("COGetArticleOrder2", AppSession.UserInfo.CustomerId);
        private void ArticleOrders_Load(object sender, EventArgs e)
        {
            MainToolbar.Visible = true;

            //DataTable dtOpen = dtAll.Select("StateId = 1").CopyToDataTable();

            gv.Init(dtAll);
            gv.InitNewRow += gv_InitNewRow;
            gv.MakeColumnInvisible("KCustomerId", "KVendorId", "AyrimlarId");
            gv.SetCaptions("ArticleOrderId:#", "ProductId:Article", "BrandId:Brand", "VendorId:Vendor", "PreferredVendorId:Preferred Vendor", "QtyTalep:QtyRequested");
            gv.FormatAsDate("RequiredETA", "ModifiedOn", "InsertedOn", "ShirtETD", "OrderDate");
            gv.FormatAsNumber(1, "QtyTalep", "QtyOrdered", "QtyShipped", "QtyArrived", "QtyMissing");
            gv.FormatAsNumber(4, "Price", "BasicShirtPrice");
            gv.AddSummary("QtyTalep", "QtyOrdered", "QtyShipped", "QtyArrived", "QtyMissing");
            gv.OptionsView.NewItemRowPosition = NewItemRowPosition.Bottom;
            gv.Columns["OrderRefId"].ColumnEdit = RepositoryManager.riOrderRef;
            gv.Columns["PreferredVendorId"].ColumnEdit = 
            gv.Columns["VendorId"].ColumnEdit = RepositoryManager.riVendor;
            gv.Columns["KCustomerId"].ColumnEdit = RepositoryManager.riCustomer;
            gv.Columns["Status"].ColumnEdit = RepositoryManager.riPODurum;
            gv.Columns["AyrimId"].ColumnEdit = RepositoryManager.riAyrim;
            gv.MakeReadOnly("NewArticle", "QtyTalep", "RequiredETA", "OrderRefId", "VendorId", "ShirtETD", "BrandId", "Notes", "Notes2", "Quality");

            gv.SetIdentity("ArticleOrderId");
            gv.DataTable().AddAutoIncrementalColumn("ArticleOrderId", -1, -1);

            gv.ShowingEditor += gv_ShowingEditor;
            gv.RowCellStyle += gv_RowCellStyle;
            gv.BestFitColumns();


            //RepositoryManager.dsi.Brand.DefaultView.RowFilter = "CustomerId=" + AppSession.UserInfo.CustomerId;
        }

        void gv_ShowingEditor(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (gv.GetFocusedDataRow() != null)
                e.Cancel = gv.GetFocusedDataRow()["ArticleOrderId"].ToIntZero() == 0;
            else
                e.Cancel = false;
        }

        void gv_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            if (e.RowHandle > -1)
            {
                int QtyArrived = gv.GetRowCellValue(e.RowHandle, "QtyArrived").ToIntZero();
                switch (e.Column.FieldName)
                {
                    case "QtyTalep":
                    case "QtyOrdered":
                        e.Appearance.Font = new Font(AppearanceObject.DefaultFont, FontStyle.Bold);
                        //if (QtyArrived>0)
                        //    e.Appearance.ForeColor = Color.Purple;
                        return;
                }
            }
        }

        void gv_InitNewRow(object sender, InitNewRowEventArgs e)
        {
            DataRowView dv = (DataRowView)gv.GetRow(e.RowHandle);
            //dv.Row["CustomerId"] = AppSession.UserInfo.CustomerId;
            //dv.Row["StateId"] = 1;
            dv.Row["InsertedOn"] = DateTime.Now;
        }

        private void btnSave_ItemClick(object sender, ItemClickEventArgs e)
        {
            int rh = gv.FocusedRowHandle;

            gv.EndEdit();

            if (gv.Update("ArticleOrderId,NewArticle,KCustomerId,KVendorId,ProductId,QtyTalep,OrderRefId,RequiredETA,ShirtETD,PreferredVendorId,Notes,Notes2,Price,Quality", "COArticleOrder") > 0)
            {
                ExecQuery("COInform", AppSession.UserInfo.CustomerId, 2);
                gv.VeriyiGuncelle(true, "COGetArticleOrder2", AppSession.UserInfo.CustomerId);
                gv.FocusedRowHandle = rh;
            }

        }

        //private void barCheckItem1_CheckedChanged(object sender, ItemClickEventArgs e)
        //{
        //    DataTable dtOpen = dtAll.Select("StateId = 1").CopyToDataTable();
        //    dtOpen.TableName = dtAll.TableName = "Table1";
        //    if (chkShowAll.Checked)
        //        gv.VeriyiGuncelle(true, dtAll);
        //    else
        //        gv.VeriyiGuncelle(true, dtOpen);
        //}
    }
}
